package com.easipass.sys.config;

import com.easipass.commons.http.HttpClientBeanHolder;
import com.easipass.commons.http.client.EpRestTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.Charset;

@Configuration
public class BeanConfig {

    @Value("${spring.application.name}")
    private String applicationName;

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(RestTemplateBuilder builder) {
        StringHttpMessageConverter m = new StringHttpMessageConverter(Charset.forName("UTF-8"));
        return builder.additionalMessageConverters(m).build();
    }

    @Bean
    public EpRestTemplate epRestTemplate(){
        EpRestTemplate epRestTemplate = HttpClientBeanHolder.getEpRestTemplate(applicationName);
        return epRestTemplate;
    }

}
